package cn.zysuyuan.common.utils;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-03-11 21:05
 */


import cn.zysuyuan.common.entity.ChartData;
import cn.zysuyuan.item.entity.DeviceData;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 此类为计算并返回用于echart图表的数据
 */
public class EchartsUtils<T> {

    /**
     *
     * @param hour  时位，即循环的极限值
     * @param interval  间隔
     * @param rows  循环的数据
     * @return
     */
    public static List<Double> getEchartsData(int hour, int interval, List<DeviceData> rows, String name ) throws Exception {
        Map<String,List<Double>> map = new HashMap<>();
        List<Double> datas = new ArrayList<>();
        for (int i = 0; i < rows.size(); i++ ) {
            ChartData chartData = new ChartData();
            // 根据数据名称获取对应的数据
            CaculateChartData.JudgeDevice(name,chartData,rows,i);
            SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
            String date = df.format(chartData.getCreateTime());
            Date time = df.parse(date);
            Calendar nowTime = Calendar.getInstance();
            // 得到数据对应的采集时间
            nowTime.setTime(time);
            for (int j = 0; j < hour + 1; j = j + interval) {
                // 循环判断
                if (!map.containsKey(Integer.toString(j))) {
                    List<Double> lists =  new ArrayList<>();
                    map.put(Integer.toString(j),lists);
                }
                String timeBefore = Integer.toString(j) + ":00:00";
                String timeAfter = Integer.toString(j+1) + ":00:00";
                if (nowTime.after(dateLine(timeBefore)) && nowTime.before(dateLine(timeAfter))) {
                    System.out.println(timeBefore + "到---" + timeAfter);
                    System.out.println("数值：" + chartData.getData());
                    TimeListUtil.setDeviceData(j,chartData.getData(),map);
                    System.out.println("当前所在小时:" + j);
                }
            }
        }
        System.out.println("map大小：" + map.size());
//        System.out.println("集合10：" + map.get("10"));
        for (int m = 0; m < hour; m = m + interval) {
            // 循环计算平均值
            DecimalFormat df = new DecimalFormat("0.0");
            Double result = CaculateAverage(map.get(Integer.toString(m)));
            Double data = 0.0;
            if (result != null) {
                data = Double.valueOf(df.format(CaculateAverage(map.get(Integer.toString(m)))));
            }else {
                data = null;
            }
            datas.add(data);
        }
        return datas;
    }

    public static Calendar dateLine(String date){
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        Calendar time = null;
        try {
            Date ctime = df.parse(date);
            time = Calendar.getInstance();
            time.setTime(ctime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    public static Double CaculateAverage(List<Double> list) {
        Double sum = 0.0;
        int num = list.size();
        for (int i = 0; i < list.size(); i++) {
            sum = sum + list.get(i);
        }
        System.out.println("总数" + sum);
        if (list.size() == 0) {
            return null;
        }else {
            return sum/num;
        }
    }

}
